﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class User : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkIsDomainUser;
        protected CheckBox chkIsWhUser;
        protected CheckBox chkSysCreated;
        protected HtmlForm F1021;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPassword;
        protected HtmlHead Head1;
        protected Label lblAddress;
        protected Label lblCity;
        protected Label lblConfirmPassword;
        protected Label lblCountry;
        protected Label lblDepatrment;
        protected Label lblEmail;
        protected Label lblExpireDate;
        protected Label lblFax;
        protected Label lblMobile;
        protected Label lblMsg;
        protected Label lblPassword;
        protected Label lblPhone;
        protected Label lblState;
        protected Label lblUser;
        protected Label lblUserID;
        protected Label lblUserName;
        protected Label lblZip;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAddress;
        protected TextBox txtCity;
        protected TextBox txtConfirmPassword;
        protected TextBox txtCountry;
        protected TextBox txtDepatrment;
        protected TextBox txtEmail;
        protected TextBox txtExpireDate;
        protected TextBox txtFax;
        protected TextBox txtMobile;
        protected TextBox txtPassword;
        protected TextBox txtPhone;
        protected TextBox txtState;
        protected TextBox txtUserID;
        protected TextBox txtUserName;
        protected TextBox txtZip;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtUserID.Text = "";
            this.txtUserID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSysCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_009");
            }
            else if (!string.IsNullOrEmpty(this.txtUserID.Text.Trim()))
            {
                try
                {
                    List<string> userID = new List<string> {
                        this.txtUserID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(userID, "USER_MST") + base.GetGlobalMsgResource("F1200_010");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1200_010") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("UserQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("UserList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtUserID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_005");
            }
            else if (string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_006");
            }
            else if (base.DataEntityViewState != null)
            {
                string strpass = "";
                for (int i = 0; this.hdnPassword.Value.Length > i; i++)
                {
                    strpass = strpass + "☀";
                }
                if ((!string.IsNullOrEmpty(this.txtPassword.Text.Trim()) && (this.txtPassword.Text != strpass)) && (this.txtPassword.Text.Trim() != this.txtConfirmPassword.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1200_007");
                }
                else
                {
                    this.UpdateUserInfo();
                }
            }
            else if (string.IsNullOrEmpty(this.txtPassword.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_008");
            }
            else if (this.txtPassword.Text.Trim() != this.txtConfirmPassword.Text.Trim())
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_007");
            }
            else
            {
                this.InsertUserInfo();
            }
        }

        public string GetLicenseContext(ref string maxUser, ref string warehouses, ref string expired, ref string activationCode)
        {
            string reg = string.Empty;
            XmlDocument doc = new XmlDocument();
            XmlNodeReader reader = null;
            string s = string.Empty;
            try
            {
                string xmlPath = base.Server.MapPath("Administration");
                xmlPath = xmlPath.Substring(0, xmlPath.LastIndexOf(@"\"));
                xmlPath = xmlPath.Substring(0, xmlPath.LastIndexOf(@"\")) + @"\License\license.qt";
                string xmlString = string.Empty;
                using (StreamReader sr = new StreamReader(xmlPath))
                {
                    xmlString = EnDeCrypt.DecryptString(sr.ReadToEnd());
                }
                doc.LoadXml(xmlString);
                reader = new XmlNodeReader(doc);
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            s = reader.Name;
                            goto Label_016A;

                        case XmlNodeType.Text:
                            if (string.Compare(s, "MaxUsers", true) != 0)
                            {
                                break;
                            }
                            maxUser = reader.Value;
                            goto Label_016A;

                        default:
                            goto Label_016A;
                    }
                    if (string.Compare(s, "Warehouses", true) == 0)
                    {
                        warehouses = reader.Value;
                    }
                    else if (string.Compare(s, "Expired", true) == 0)
                    {
                        expired = reader.Value;
                    }
                    else if (string.Compare(s, "ActivationCode", true) == 0)
                    {
                        activationCode = reader.Value;
                    }
                Label_016A:;
                }
            }
            catch (Exception ex)
            {
                reg = ex.Message;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return reg;
        }

        private void InsertUserInfo()
        {
            string reg = string.Empty;
            string MaxUsers = "0";
            string Warehouses = string.Empty;
            string Expired = string.Empty;
            string ActivationCode = string.Empty;
            reg = this.GetLicenseContext(ref MaxUsers, ref Warehouses, ref Expired, ref ActivationCode);
            try
            {
                UserMstInfo userInfo = new UserMstInfo {
                    UserID = this.txtUserID.Text,
                    UserName = this.txtUserName.Text,
                    Zip = this.txtZip.Text,
                    Address = this.txtAddress.Text,
                    Country = this.txtCountry.Text,
                    Email = this.txtEmail.Text,
                    Fax = this.txtFax.Text
                };
                if (!string.IsNullOrEmpty(this.txtExpireDate.Text))
                {
                    userInfo.ExpireDate = Convert.ToDateTime(this.txtExpireDate.Text);
                }
                userInfo.Passwd = this.txtPassword.Text;
                userInfo.Phone = this.txtPhone.Text;
                userInfo.City = this.txtCity.Text;
                userInfo.State = this.txtState.Text;
                userInfo.Mobile = this.txtMobile.Text;
                userInfo.Department = this.txtDepatrment.Text;
                userInfo.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
                userInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                userInfo.IsDomainUser = this.chkIsDomainUser.Checked ? "Y" : "N";
                userInfo.IsWhUser = this.chkIsWhUser.Checked ? "Y" : "N";
                userInfo.MaxUsers = int.Parse(MaxUsers);
                userInfo.CreatedBy = base.UserInfo.UserID;
                userInfo.Status = 1;
                userInfo.IsLocked = "N";
                userInfo.IsLogin = "N";
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(userInfo, "USER_MST") + base.GetGlobalMsgResource("F1200_002");
                this.hdnPassword.Value = this.txtPassword.Text;
                base.DataEntityViewState = userInfo;
                this.txtUserID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_001") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetUserInfo2Form();
            }
            else
            {
                this.txtExpireDate.Text = DateTime.Now.AddMonths(3).ToShortDateString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = string.Empty;
        }

        private void SetUserInfo2Form()
        {
            try
            {
                string userID = base.Request.QueryString["ID"].ToString();
                UserMstInfo user = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(userID, "USER_MST") as UserMstInfo;
                base.DataEntityViewState = user;
                if (user != null)
                {
                    this.txtUserID.Text = user.UserID;
                    this.txtUserName.Text = user.UserName;
                    this.txtZip.Text = user.Zip;
                    this.txtAddress.Text = user.Address;
                    this.txtCountry.Text = user.Country;
                    this.txtEmail.Text = user.Email;
                    this.txtFax.Text = user.Fax;
                    this.txtExpireDate.Text = base.GetPreferenceShortDate(user.ExpireDate);
                    this.hdnPassword.Value = user.Passwd;
                    string strPass = "";
                    for (int i = 0; this.hdnPassword.Value.Length > i; i++)
                    {
                        strPass = strPass + "☀";
                    }
                    this.txtPassword.Attributes.Add("value", strPass);
                    this.txtPhone.Text = user.Phone;
                    this.txtState.Text = user.State;
                    this.txtDepatrment.Text = user.Department;
                    this.txtCity.Text = user.City;
                    this.txtMobile.Text = user.Mobile;
                    this.chkIsActive.Checked = user.IsActive == "Y";
                    this.chkSysCreated.Checked = user.SystemCreated == "Y";
                    this.chkIsDomainUser.Checked = user.IsDomainUser == "Y";
                    this.chkIsWhUser.Checked = user.IsWhUser == "Y";
                    this.txtUserID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateUserInfo()
        {
            try
            {
                UserMstInfo userInfo = base.DataEntityViewState as UserMstInfo;
                userInfo.UserName = this.txtUserName.Text;
                userInfo.Zip = this.txtZip.Text;
                userInfo.Address = this.txtAddress.Text;
                userInfo.Country = this.txtCountry.Text;
                userInfo.Email = this.txtEmail.Text;
                userInfo.Fax = this.txtFax.Text;
                string strPass = string.Empty;
                for (int i = 0; this.hdnPassword.Value.Length > i; i++)
                {
                    strPass = strPass + "☀";
                }
                if (!(string.IsNullOrEmpty(this.txtPassword.Text.Trim()) || !(this.txtPassword.Text != strPass)))
                {
                    userInfo.Passwd = this.txtPassword.Text;
                    this.hdnPassword.Value = this.txtPassword.Text;
                    userInfo.ExpireDate = DateTime.Now.AddMonths(3);
                }
                else
                {
                    userInfo.Passwd = this.hdnPassword.Value;
                }
                userInfo.Phone = this.txtPhone.Text;
                userInfo.City = this.txtCity.Text;
                userInfo.State = this.txtState.Text;
                userInfo.Mobile = this.txtMobile.Text;
                userInfo.Department = this.txtDepatrment.Text;
                userInfo.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
                userInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                userInfo.IsDomainUser = this.chkIsDomainUser.Checked ? "Y" : "N";
                userInfo.IsWhUser = this.chkIsWhUser.Checked ? "Y" : "N";
                userInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(userInfo, "USER_MST") + base.GetGlobalMsgResource("F1200_003");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1200_004") + ex.Message;
            }
        }
    }
}

